/**********************************************************************
*
*   Copyright(C) 2021
*   FileName:
*   Version : V0.0.1
*   Date    : 2024-04-13
*   Author  : Song.YuLong@chipsbank
*   Description: 显示屏SPI接口驱动
*
*   Others:
*
*   Function List:
*   History      :
*
***********************************************************************/

#include "app_config.h"
#include "debug.h"

#include "STC32G.H"
#include "STC32G_GPIO.h"
#include "STC32G_Delay.h"
#include "STC32G_SPI.h"
#include "STC32G_Clock.h"
#include "STC32G_NVIC.h"
#include "STC32G_Switch.h"
#include "Type_def.h"

#include "lcd_driver_spi.h"

#if TCFG_DRV_LCD_SPI_SUPPORT

#define LCD_DS          P20
#define LCD_DS_LOW()    LCD_DS=0;
#define LCD_DS_HIGH()   LCD_DS=1;


#define LCD_RESET           P21
#define LCD_RESET_LOW()     LCD_RESET=0;
#define LCD_RESET_HIGH()    LCD_RESET=1;


#define LCD_CS              P22
#define LCD_SPI_SELECT()     LCD_CS = 0;
#define LCD_SPI_DESELECT()   LCD_CS = 1;
#define LCD_SPI_NUM         SPI1  

// sbit  SPI_SS_2    = P2^2;
// sbit  SPI_MOSI_2  = P2^3;
// sbit  SPI_MISO_2  = P2^4;
// sbit  SPI_SCLK_2  = P2^5;

#if (LCD_PANLE_MODULE == LCD_MODULE_GC9307)  //GC9307
void GC9307_Init(void);
#endif
#if (LCD_PANLE_MODULE == LCD_MODULE_ST7735S) //ST7735S/ST7789S LCM
void ST7735S_Init(void);
#endif
#if (LCD_PANLE_MODULE == LCD_MODULE_ST7567) // ST7567 LCM
void ST7567_Init(void);
void ST7567_Sleep(void);
#endif
#if (LCD_PANLE_MODULE == LCD_MODULE_UC1705X) // UC1705X LCM
void UC1705X_Init(void);
void UC1705X_Sleep(void);
#endif
#if (LCD_PANLE_MODULE == LCD_MODULE_ST7571) // 2.3寸  128x128 点阵灰阶屏
void ST7571_Init(void);
void ST7571_Sleep(void);
#endif
#if (LCD_PANLE_MODULE == LCD_MODULE_UC1611S)
void UC1611S_Init(void);
void UC1611S_Sleep(void);
#endif

/**
 * @brief   背光控制
 * @param   onoff: 1:背光亮， 0：背光灭
 * @return  void
*/



/**
 * @brief   SPI写字节
 * @param   spilcd_dev:LCD设备句柄
 * @param   data:数据
 * @param   mode: 0,写入命令，1，写入数据
*/
u8 spilcd_spi_write(u8 tdata, spilcd_rw_mode_e mode)
{
    if(mode == SPILCD_RW_CMD) {
        // TODO:写入命令
        LCD_DS_LOW();
	} else if(mode == SPILCD_RW_DATA) {
        // TODO:写入数据
        LCD_DS_HIGH();
	}
    
    LCD_SPI_SELECT();
    // SPI_SetMode(SPI_Mode_Master);
    SPI_WriteByte(tdata);
    LCD_SPI_DESELECT();
    LCD_DS_HIGH();


#if 0
    local_irq_disable();
    if(mode == SPILCD_RW_CMD) {
        // TODO:写入命令
        xa_gpio_output_value(spilcd_dev->lcd_ds, 0);
	} else if(mode == SPILCD_RW_DATA) {
        // TODO:写入数据
        xa_gpio_output_value(spilcd_dev->lcd_ds, 1);
	}

    xa_gpio_output_value(spilcd_dev->lcd_cs, 0);
    soft_spi_send_byte(spilcd_dev->spi_num, data);
    xa_gpio_output_value(spilcd_dev->lcd_cs, 1);
    local_irq_enable();
#endif
    return 0;
}

#if 0
u8 spilcd_spi_write_buffer(void, uint8_t *buf, uint32_t len)
{
    local_irq_disable();
    xa_gpio_output_value(spilcd_dev->lcd_cs, 0);
    soft_spi_dma_send(spilcd_dev->spi_num, buf, len);
    xa_gpio_output_value(spilcd_dev->lcd_cs, 1);
    local_irq_enable();

    return 0;
}
#endif

void spilcd_set_window(int x0, int y0, int x1, int y1)
{
#if (LCD_PANLE_W160H80)
	spilcd_spi_write(0x2B, SPILCD_RW_CMD);
	spilcd_spi_write(0x00, SPILCD_RW_DATA);
	spilcd_spi_write(0x1A, SPILCD_RW_DATA);
	spilcd_spi_write(0x00, SPILCD_RW_DATA);
	spilcd_spi_write(0x69, SPILCD_RW_DATA);

	spilcd_spi_write(0x2A, SPILCD_RW_CMD);
	spilcd_spi_write(0x00, SPILCD_RW_DATA);
	spilcd_spi_write(0x01, SPILCD_RW_DATA);
	spilcd_spi_write(0x00, SPILCD_RW_DATA);
	spilcd_spi_write(0xA0, SPILCD_RW_DATA);
#elif (LCD_PANLE_W80H160)
	spilcd_spi_write(0x2A, SPILCD_RW_CMD);
	spilcd_spi_write(0x00, SPILCD_RW_DATA);
	spilcd_spi_write(0x1A, SPILCD_RW_DATA);
	spilcd_spi_write(0x00, SPILCD_RW_DATA);
	spilcd_spi_write(0x69, SPILCD_RW_DATA);

	spilcd_spi_write(0x2B, SPILCD_RW_CMD);
	spilcd_spi_write(0x00, SPILCD_RW_DATA);
	spilcd_spi_write(0x01, SPILCD_RW_DATA);
	spilcd_spi_write(0x00, SPILCD_RW_DATA);
	spilcd_spi_write(0xA0, SPILCD_RW_DATA);
#endif
	spilcd_spi_write(0x2C, SPILCD_RW_CMD);
}

#if (LCD_PANLE_MODULE == LCD_MODULE_GC9307)
void GC9307_Init()
{
	lcd_debug("GC9307_Init\n");
    // for GC9307 reset
    LCD_RESET_HIGH();
    delay_ms(40);
    LCD_RESET_LOW();
    delay_ms(120);
    LCD_RESET_HIGH();
    delay_ms(150);

    // 写入初始化寄存器
}
#endif



#if (LCD_PANLE_MODULE == LCD_MODULE_ST7735S) //ST7735S/ST7789S LCM
void ST7735S_Init(void)
{
	log_d("ST7735S_Init\n");
    // for ST7735S reset
    LCD_RESET_HIGH();
    delay_ms(40);
    LCD_RESET_LOW();
    delay_ms(120);
    LCD_RESET_HIGH();
    delay_ms(150);

    spilcd_spi_write(0x11, SPILCD_RW_CMD);
    delay_ms(100);

    spilcd_spi_write(0x21, SPILCD_RW_CMD);

    spilcd_spi_write(0xB1, SPILCD_RW_CMD);
    spilcd_spi_write(0x05, SPILCD_RW_DATA);
    spilcd_spi_write(0x3A, SPILCD_RW_DATA);
    spilcd_spi_write(0x3A, SPILCD_RW_DATA);

    spilcd_spi_write(0xB2, SPILCD_RW_CMD);
    spilcd_spi_write(0x05, SPILCD_RW_DATA);
    spilcd_spi_write(0x3A, SPILCD_RW_DATA);
    spilcd_spi_write(0x3A, SPILCD_RW_DATA);

    spilcd_spi_write(0xB3, SPILCD_RW_CMD);
    spilcd_spi_write(0x05, SPILCD_RW_DATA);
    spilcd_spi_write(0x3A, SPILCD_RW_DATA);
    spilcd_spi_write(0x3A, SPILCD_RW_DATA);
    spilcd_spi_write(0x05, SPILCD_RW_DATA);
    spilcd_spi_write(0x3A, SPILCD_RW_DATA);
    spilcd_spi_write(0x3A, SPILCD_RW_DATA);

    spilcd_spi_write(0xB4, SPILCD_RW_CMD);
    spilcd_spi_write(0x03, SPILCD_RW_DATA);

    spilcd_spi_write(0xC0, SPILCD_RW_CMD);
    spilcd_spi_write(0x62, SPILCD_RW_DATA);
    spilcd_spi_write(0x02, SPILCD_RW_DATA);
    spilcd_spi_write(0x04, SPILCD_RW_DATA);

    spilcd_spi_write(0xC1, SPILCD_RW_CMD);
    spilcd_spi_write(0xC0, SPILCD_RW_DATA);

    spilcd_spi_write(0xC2, SPILCD_RW_CMD);
    spilcd_spi_write(0x0D, SPILCD_RW_DATA);
    spilcd_spi_write(0x00, SPILCD_RW_DATA);

    spilcd_spi_write(0xC3, SPILCD_RW_CMD);
    spilcd_spi_write(0x8D, SPILCD_RW_DATA);
    spilcd_spi_write(0x6A, SPILCD_RW_DATA);

    spilcd_spi_write(0xC4, SPILCD_RW_CMD);
    spilcd_spi_write(0x8D, SPILCD_RW_DATA);
    spilcd_spi_write(0xEE, SPILCD_RW_DATA);

    spilcd_spi_write(0xC5, SPILCD_RW_CMD);  /*VCOM*/
    spilcd_spi_write(0x0E, SPILCD_RW_DATA);

    spilcd_spi_write(0xE0, SPILCD_RW_CMD);
    spilcd_spi_write(0x10, SPILCD_RW_DATA);
    spilcd_spi_write(0x0E, SPILCD_RW_DATA);
    spilcd_spi_write(0x02, SPILCD_RW_DATA);
    spilcd_spi_write(0x03, SPILCD_RW_DATA);
    spilcd_spi_write(0x0E, SPILCD_RW_DATA);
    spilcd_spi_write(0x07, SPILCD_RW_DATA);
    spilcd_spi_write(0x02, SPILCD_RW_DATA);
    spilcd_spi_write(0x07, SPILCD_RW_DATA);
    spilcd_spi_write(0x0A, SPILCD_RW_DATA);
    spilcd_spi_write(0x12, SPILCD_RW_DATA);
    spilcd_spi_write(0x27, SPILCD_RW_DATA);
    spilcd_spi_write(0x37, SPILCD_RW_DATA);
    spilcd_spi_write(0x00, SPILCD_RW_DATA);
    spilcd_spi_write(0x0D, SPILCD_RW_DATA);
    spilcd_spi_write(0x0E, SPILCD_RW_DATA);
    spilcd_spi_write(0x10, SPILCD_RW_DATA);

    spilcd_spi_write(0xE1, SPILCD_RW_CMD);
    spilcd_spi_write(0x10, SPILCD_RW_DATA);
    spilcd_spi_write(0x0E, SPILCD_RW_DATA);
    spilcd_spi_write(0x03, SPILCD_RW_DATA);
    spilcd_spi_write(0x03, SPILCD_RW_DATA);
    spilcd_spi_write(0x0F, SPILCD_RW_DATA);
    spilcd_spi_write(0x06, SPILCD_RW_DATA);
    spilcd_spi_write(0x02, SPILCD_RW_DATA);
    spilcd_spi_write(0x08, SPILCD_RW_DATA);
    spilcd_spi_write(0x0A, SPILCD_RW_DATA);
    spilcd_spi_write(0x13, SPILCD_RW_DATA);
    spilcd_spi_write(0x26, SPILCD_RW_DATA);
    spilcd_spi_write(0x36, SPILCD_RW_DATA);
    spilcd_spi_write(0x00, SPILCD_RW_DATA);
    spilcd_spi_write(0x0D, SPILCD_RW_DATA);
    spilcd_spi_write(0x0E, SPILCD_RW_DATA);
    spilcd_spi_write(0x10, SPILCD_RW_DATA);

    spilcd_spi_write(0x3A, SPILCD_RW_CMD);
    spilcd_spi_write(0x05, SPILCD_RW_DATA); //05:RGB565,

    #if (LCD_PANLE_W160H80)
    spilcd_spi_write(0x36, SPILCD_RW_CMD);
    spilcd_spi_write(0xa8, SPILCD_RW_DATA);   /*0x28  0x08 0xc8   MY MX MV ML, RGB/BGR MH X X*/   //MH:Horizontal Order 0:left to right, 1:right to left
    #elif (LCD_PANLE_W80H160)
    spilcd_spi_write(0x36, SPILCD_RW_CMD);
    spilcd_spi_write(0xc8, SPILCD_RW_DATA);   /*0x28  0x08 0xc8   MY MX MV ML, RGB/BGR MH X X*/   //MH:Horizontal Order 0:left to right, 1:right to left
    #endif

    spilcd_spi_write(0x29, SPILCD_RW_CMD);
}
#endif


#if (LCD_PANLE_MODULE == LCD_MODULE_ST7567) // ST7567 LCM

void ST7567_Init(void)
{
	log_d("ST7567_Init\n");
    // for ST7567 reset

    LCD_RESET_HIGH();
    delay_ms(50);
    LCD_RESET_LOW();
    delay_ms(50);
    LCD_RESET_HIGH();
    delay_ms(150);

    spilcd_spi_write(0xE2, SPILCD_RW_CMD);  /* 软复位*/
    delay_ms(50);
    spilcd_spi_write(0x2C, SPILCD_RW_CMD);   /* 升压步骤 1*/ 
    delay_ms(50);
    spilcd_spi_write(0x2E, SPILCD_RW_CMD);   /* 升压步骤 2*/ 
    delay_ms(50);
    spilcd_spi_write(0x2F, SPILCD_RW_CMD);   /* 升压步骤 3*/ 
    delay_ms(50);


	spilcd_spi_write(0x24, SPILCD_RW_CMD);   /* 调整对比度，设置范围0x20～0x27  */
	spilcd_spi_write(0x81, SPILCD_RW_CMD);   /* 微调对比度 */
    spilcd_spi_write(0x1B, SPILCD_RW_CMD);   /* 微调对比度的值 */
    spilcd_spi_write(0xA3, SPILCD_RW_CMD);   /*  偏压比（bias）,0xA2:1/9   0xA3:1=1/7   */

	spilcd_spi_write(0xa0, SPILCD_RW_CMD);//set seg direction
	spilcd_spi_write(0xc8, SPILCD_RW_CMD);//set com direction
	spilcd_spi_write(0x40, SPILCD_RW_CMD);//set scroll line
    
	spilcd_spi_write(0x2f, SPILCD_RW_CMD);//set power control
	spilcd_spi_write(0x81, SPILCD_RW_CMD);//SET ELECTRONIC VOLUME
	spilcd_spi_write(0x18, SPILCD_RW_CMD);//set pm: 通过改变这里的数值来改变电压 def:0x20,调节对比度，小：暗，大：亮

	spilcd_spi_write(0xa6, SPILCD_RW_CMD);//set inverse display	   a6 off, a7 on
	spilcd_spi_write(0xa4, SPILCD_RW_CMD);//set all pixel on
	spilcd_spi_write(0xaf, SPILCD_RW_CMD);//set display enable
}

void ST7567_Sleep(void)
{
	log_d("UC1705X_Sleep\n");
	spilcd_spi_write(0xAE, SPILCD_RW_CMD);//set display off
	spilcd_spi_write(0xA5, SPILCD_RW_CMD);//set all pixel on
}
#endif

#if (LCD_PANLE_MODULE == LCD_MODULE_UC1705X) // UC1705X LCM
void UC1705X_Init(void)
{
	log_d("UC1705X_Init\n");
    // for UC1705X reset

    LCD_RESET_HIGH();
    delay_ms(10);
    LCD_RESET_LOW();
    delay_ms(50);
    LCD_RESET_HIGH();
    delay_ms(120);

#if 1
    spilcd_spi_write(0xe2, SPILCD_RW_CMD);            			/* soft reset */
    delay_ms(10);
    spilcd_spi_write(0xae, SPILCD_RW_CMD);		                /* display off */
    spilcd_spi_write(0x40, SPILCD_RW_CMD);		                /* set display start line to 0 */
    spilcd_spi_write(0xa0, SPILCD_RW_CMD);		                /* ADC set to reverse */	// 从左往后，还是从右往左扫描(0xa1)
    spilcd_spi_write(0xc8, SPILCD_RW_CMD);		                /* common output mode */	// 从上往下，还是从下往上扫描(0xc0)
    spilcd_spi_write(0xa6, SPILCD_RW_CMD);		                /* display normal, bit val 0: LCD pixel off. */
    spilcd_spi_write(0xa2, SPILCD_RW_CMD);		                /* LCD bias 1/9 */
    spilcd_spi_write(0x2f, SPILCD_RW_CMD);		                /* all power  control circuits on */
    spilcd_spi_write(0xf8, SPILCD_RW_CMD);						/* set booster ratio to */
    spilcd_spi_write(0x00, SPILCD_RW_CMD);						/* 4x */
    spilcd_spi_write(0x23, SPILCD_RW_CMD);						/* set V0 voltage resistor ratio to large */
    spilcd_spi_write(0x81, SPILCD_RW_CMD);						/* set contrast */
    spilcd_spi_write(0x27, SPILCD_RW_CMD);						/* contrast value */
    spilcd_spi_write(0xac, SPILCD_RW_CMD);						/* indicator */
    spilcd_spi_write(0xae, SPILCD_RW_CMD);		                /* display off */
    spilcd_spi_write(0xa5, SPILCD_RW_CMD);		                /* enter powersafe: all pixel on, issue 142 */
	
	spilcd_spi_write(0xa4, SPILCD_RW_CMD);
	spilcd_spi_write(0xaf, SPILCD_RW_CMD);
#endif

#if 0
    spilcd_spi_write(0xE2, SPILCD_RW_CMD); // Software Reset
    delay_ms(50);

	spilcd_spi_write(0x24, SPILCD_RW_CMD);//SET VLCD RESISTOR RATIO
	spilcd_spi_write(0xa2, SPILCD_RW_CMD);//BR=1/9
	spilcd_spi_write(0xa0, SPILCD_RW_CMD);//set seg direction
	spilcd_spi_write(0xc8, SPILCD_RW_CMD);//set com direction
	spilcd_spi_write(0x2f, SPILCD_RW_CMD);//set power control
	spilcd_spi_write(0x40, SPILCD_RW_CMD);//set scroll line
	spilcd_spi_write(0x81, SPILCD_RW_CMD);//SET ELECTRONIC VOLUME
	spilcd_spi_write(0x18, SPILCD_RW_CMD);//set pm: 通过改变这里的数值来改变电压 def:0x20,调节对比度，小：暗，大：亮

	// spilcd_spi_write(0xa6, SPILCD_RW_CMD);//set inverse display	   a6 off, a7 on
	// spilcd_spi_write(0xa4, SPILCD_RW_CMD);//set all pixel on

	spilcd_spi_write(0xaf, SPILCD_RW_CMD);//set display enable
#endif
}

void UC1705X_Sleep(void)
{
	log_d("UC1705X_Sleep\n");
	spilcd_spi_write(0xAE, SPILCD_RW_CMD);//set display off
	spilcd_spi_write(0xA5, SPILCD_RW_CMD);//set all pixel on
}
#endif

#if (LCD_PANLE_MODULE == LCD_MODULE_ST7571) // 2.3寸  128x128 点阵灰阶屏
void ST7571_Init(void)
{
	log_d("UC1705X_Init\n");
    // for UC1705X reset

    // LCD_RESET_HIGH();
    // delay_ms(50);
    LCD_RESET_LOW();
    delay_ms(50);
    LCD_RESET_HIGH();
    delay_ms(150);

    spilcd_spi_write(0xE2, SPILCD_RW_CMD); // Software Reset
    delay_ms(100);
    spilcd_spi_write(0xAE, SPILCD_RW_CMD); // Display off, 0xAF=On, 0xAE=Off

    spilcd_spi_write(0x38, SPILCD_RW_CMD);  // 设置模式
    spilcd_spi_write(0x94, SPILCD_RW_CMD);  // 帧频率 = 75Hz  升压效率水平=2
    
    spilcd_spi_write(0xA0, SPILCD_RW_CMD);  // 设置SEG列扫描方向 0xA0=正常方向(SEG0-SEG127)  0xA1=反方向扫描(SEG127-SEG0)
    spilcd_spi_write(0xC8, SPILCD_RW_CMD);  // 设置COM行扫描方向 0XC0=正常方向(COM0-COM127)  0xC8=反方向扫描(COM127-COM0)
    
    spilcd_spi_write(0x44, SPILCD_RW_CMD);  // 设置COM0，将某个COM行指定为屏幕上的COM0行
    spilcd_spi_write(0x00, SPILCD_RW_CMD);  // 从第0行开始显示数据
    
    spilcd_spi_write(0x40, SPILCD_RW_CMD);  // 指定DDRAM第一显示行的行地址
    spilcd_spi_write(0x00, SPILCD_RW_CMD);  // 将第0行设置为第一显示行

    spilcd_spi_write(0xAB, SPILCD_RW_CMD);  // 开启内部震荡器
    spilcd_spi_write(0x27, SPILCD_RW_CMD);  // 选择内部调节器，用于调节电压
    
    spilcd_spi_write(0x81, SPILCD_RW_CMD);  // 设置对比度，取值范围 0~63
    spilcd_spi_write(  30, SPILCD_RW_CMD);  // 对比度设置为 30 对比度值为十进制

    spilcd_spi_write(0x57, SPILCD_RW_CMD);  // 选择LCD液晶显示器 Bias = 1/12

    spilcd_spi_write(0x48, SPILCD_RW_CMD);  // 设置Duty,取值范围 0 - 255
    spilcd_spi_write(0x61, SPILCD_RW_CMD);  // 设置Duty = 1/98

    // 不能同时打开3个电源，要分别打开
    spilcd_spi_write(0x2C, SPILCD_RW_CMD);  // 电源控制，打开电压转换器，VC:ON, VR: OFF, VF:OFF
    delay_ms(100);
    spilcd_spi_write(0x2E, SPILCD_RW_CMD);  // 电源控制，打开电压转换器，VC:ON, VR: ON , VF:OFF
    delay_ms(100);
    spilcd_spi_write(0x2F, SPILCD_RW_CMD);  // 电源控制，打开电压转换器，VC:ON, VR: ON , VF:ON
    delay_ms(100);

    spilcd_spi_write(0x7B, SPILCD_RW_CMD);  // 进入扩展命令设置集3（用于设置显示模式）
    spilcd_spi_write(0x10, SPILCD_RW_CMD);  // 设置颜色模式：0x11=黑白模式， 0x10=灰度模式（默认）
    spilcd_spi_write(0x00, SPILCD_RW_CMD);  // 退出扩展命令设置集3

    spilcd_spi_write(0xA6, SPILCD_RW_CMD);  // 反显开关 0xA6=关闭反显， 0xA7=打开反显
    spilcd_spi_write(0xA4, SPILCD_RW_CMD);  // 显示所有像素 0xA4=关（正常显示） 0xA5=开（所有点显示黑色）
    spilcd_spi_write(0xAF, SPILCD_RW_CMD);  // 屏幕显示开关 0xAF=开，0xAE=关
}

void ST7571_Sleep(void)
{
	log_d("UC1705X_Sleep\n");
	spilcd_spi_write(0xAE, SPILCD_RW_CMD);//set display off
	spilcd_spi_write(0xA5, SPILCD_RW_CMD);//set all pixel on
}
#endif

#if (LCD_PANLE_MODULE==LCD_MODULE_UC1611S)
void UC1611S_Init(void)
{
    log_d("UC1611S_Init\n");
    LCD_RESET_LOW();
    delay_ms(100);
    LCD_RESET_HIGH();
    delay_ms(50);

    //Driver IC:ULTRACHIP UC1611S
    //Initialize 240*160 STN LCD;
    spilcd_spi_write(0xE2, SPILCD_RW_CMD); // Software Reset
    delay_ms(100);
    //spilcd_spi_write(0x2f, SPILCD_RW_CMD);          // Set Power Control  loading 15-24nF  
    
    spilcd_spi_write(0x24, SPILCD_RW_CMD);            // Set TC, Temperature Compensation 
	
	spilcd_spi_write(0xc0, SPILCD_RW_CMD);            //set LC MX MY
    spilcd_spi_write(0x04, SPILCD_RW_CMD);            //
	
#if USE_DOWN_TO_UP   
	spilcd_spi_write(0xc0, SPILCD_RW_CMD);            //set LC MX MY
    spilcd_spi_write(0x02, SPILCD_RW_CMD);
#else
    spilcd_spi_write(0xc0, SPILCD_RW_CMD);            //set LC MX MY
    spilcd_spi_write(0x04, SPILCD_RW_CMD);
#endif
	
    spilcd_spi_write(0xa6, SPILCD_RW_CMD);            // a3 Set Inverse Display A7反色  
    spilcd_spi_write(0x2c, SPILCD_RW_CMD);
	spilcd_spi_write(0x2e, SPILCD_RW_CMD);
	spilcd_spi_write(0x2f, SPILCD_RW_CMD);
   
    spilcd_spi_write(0xea, SPILCD_RW_CMD);            // Set Bias Ratio : 1/6   
    spilcd_spi_write(0x81, SPILCD_RW_CMD);            // Set Vbias 
    spilcd_spi_write(0x85, SPILCD_RW_CMD);            //     
    spilcd_spi_write(0x89, SPILCD_RW_CMD);
		
	spilcd_spi_write(0x40, SPILCD_RW_CMD);
	spilcd_spi_write(0x50, SPILCD_RW_CMD);
	spilcd_spi_write(0x90, SPILCD_RW_CMD);
		
	spilcd_spi_write(0xf1, SPILCD_RW_CMD);               
    spilcd_spi_write(0x9f, SPILCD_RW_CMD);            // Set COM End (159)   
    
	spilcd_spi_write(0x72, SPILCD_RW_CMD);
	spilcd_spi_write(0x60, SPILCD_RW_CMD);
	spilcd_spi_write(0x10, SPILCD_RW_CMD);
	spilcd_spi_write(0x00, SPILCD_RW_CMD);
		
	spilcd_spi_write(0xf8, SPILCD_RW_CMD);		

	spilcd_spi_write(0xf4, SPILCD_RW_CMD);
	spilcd_spi_write(0x00, SPILCD_RW_CMD);
	spilcd_spi_write(0xf6, SPILCD_RW_CMD);
	spilcd_spi_write(0xef, SPILCD_RW_CMD);
		
	spilcd_spi_write(0x82, SPILCD_RW_CMD);
	spilcd_spi_write(0x13, SPILCD_RW_CMD);
	spilcd_spi_write(0x01, SPILCD_RW_CMD);
		
	spilcd_spi_write(0x82, SPILCD_RW_CMD);
	spilcd_spi_write(0x14, SPILCD_RW_CMD);
	spilcd_spi_write(0x00, SPILCD_RW_CMD);
		
	spilcd_spi_write(0xd1, SPILCD_RW_CMD);
	spilcd_spi_write(0xa9, SPILCD_RW_CMD);

}

void UC1611S_Sleep(void)
{
    log_d("UC1611S_Sleep\n");

}
#endif 

void spilcd_spi_init(void)
{ 
    SPI_InitTypeDef		SPI_InitStructure;
        
    // LCD GPIO 初始化
    // P2_MODE_IO_PU(GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);
    P2_MODE_OUT_PP(GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);
    P2_DRIVE_HIGH(GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);
    // P2_PULL_UP_ENABLE(GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);

    P2_SPEED_HIGH(GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5);

    // LCD SPI 初始化
	SPI_InitStructure.SPI_Enable    = ENABLE;				//SPI启动    ENABLE, DISABLE
	SPI_InitStructure.SPI_SSIG      = ENABLE;				//片选位     ENABLE(忽略SS引脚功能), DISABLE(SS确定主机从机)
	SPI_InitStructure.SPI_FirstBit  = SPI_MSB;				//移位方向   SPI_MSB, SPI_LSB
	SPI_InitStructure.SPI_Mode      = SPI_Mode_Master;		//主从选择   SPI_Mode_Master, SPI_Mode_Slave
	SPI_InitStructure.SPI_CPOL      = SPI_CPOL_High;		//时钟相位   SPI_CPOL_High,   SPI_CPOL_Low
	SPI_InitStructure.SPI_CPHA      = SPI_CPHA_2Edge;		//数据边沿   SPI_CPHA_1Edge,  SPI_CPHA_2Edge
	SPI_InitStructure.SPI_Speed     = SPI_Speed_4;			//SPI速度    SPI_Speed_4, SPI_Speed_8, SPI_Speed_16, SPI_Speed_2
	SPI_Init(&SPI_InitStructure);
	NVIC_SPI_Init(ENABLE,Priority_0);		//中断使能, ENABLE/DISABLE; 优先级(低到高) Priority_0,Priority_1,Priority_2,Priority_3
	SPI_ClearFlag();   //清除 SPIF和WCOL 标志

	HSPllClkConfig(MCLKSEL_HIRC,PLL_96M, 2);    //系统时钟选择,PLL时钟选择,时钟分频系数
	HSSPI_Enable();

    SPI_SW(SPI_P22_P23_P24_P25);	//SPI_P54_P13_P14_P15,SPI_P22_P23_P24_P25,SPI_P54_P40_P41_P43,SPI_P35_P34_P33_P32
	// SPI_SS_2 = 1;
    // SPI_MOSI_2 = 1;
    // SPI_MISO_2 = 1;
    // SPI_SCLK_2 = 0;

    // SPI_SetMode(SPI_Mode_Master);
    
    // LCD 初始化寄存器
    #if (LCD_PANLE_MODULE == LCD_MODULE_ST7735S) //ST7735S/ST7789S LCM
    ST7735S_Init();
    #elif (LCD_PANLE_MODULE == LCD_MODULE_GC9307)
    GC9307_Init();
    #elif (LCD_PANLE_MODULE == LCD_MODULE_UC1705X)
    UC1705X_Init();
    #elif (LCD_PANLE_MODULE == LCD_MODULE_ST7567) // ST7567 LCM
    ST7567_Init();
    #elif (LCD_PANLE_MODULE == LCD_MODULE_SSD1309)
    SSD1309_Init();
    #elif (LCD_PANLE_MODULE == LCD_MODULE_ST7571) // 2.3寸  128x128 点阵灰阶屏
    ST7571_Init();
    #elif (LCD_PANLE_MODULE==LCD_MODULE_UC1611S)    // 240x160 单色点阵屏
    UC1611S_Init();
    #else
    #error "spilcd init LCD_PANLE_MODULE not configure."
    #endif
}


void spilcd_spi_deinit(void)
{
    #if (LCD_PANLE_MODULE == LCD_MODULE_ST7735S) //ST7735S/ST7789S LCM

    #elif (LCD_PANLE_MODULE == LCD_MODULE_GC9307)

	#elif (LCD_PANLE_MODULE == LCD_MODULE_UC1705X)
	UC1705X_Sleep();
    #elif (LCD_PANLE_MODULE == LCD_MODULE_ST7567) // ST7567 LCM
    ST7567_Sleep();
    #elif (LCD_PANLE_MODULE == LCD_MODULE_ST7571) // 2.3寸  128x128 点阵灰阶屏
    ST7571_Sleep();
    #elif (LCD_PANLE_MODULE==LCD_MODULE_UC1611S)    // 240x160 单色点阵屏
    UC1611S_Sleep();
    #else
    #error "spilcd deinit LCD_PANLE_MODULE not configure."
    #endif

}
#endif
